#include "storage.h"


Storage::Storage(void) {
	ifstream readFile;
	readFile.open(FILENAME ???);

	string data;
	Task task;
	while(getline(readFile,data)) {
		task = convertToTask(data);
		_store.push_back(task);
	}
}


Storage::~Storage(void) {

}

vector <Task> Storage::getStorage() {
	return _store;
}

vector <string> Storage::start(Task userTask) {
	_userTask = userTask;
	executeCommand();
	return _forOutputBox;
}

void Storage::executeCommand() {

	// depending _command in _userTask,
	// execute different functions accordingly
	//
	// storage class only has 
	//		add
	//		delete
	//		archive
	//		modify
	//
}

void Storage::addTask() {

	_store.push_back(_userTask);
	string data = convertToString(_userTask);

	ofstream outStream;
	outStream.open("schedule.txt", ofstream::out | ofstream::app); // random whatever name
	outStream<< data <<endl;
	outStream.close();
}
	
void Storage::deleteTask() {
	
	// delete the line from _store
	// and txt file
	// match using _taskName, not _taskLine because 
	// user may have input task line according to his search
	// and our storage line WILL NOT coincide with his line
	// the task name has already been input into _userTask

	// copy the current data over to _previousData, in case user wants to undo
	// then modify the current data
}

void Storage::modify() {
	//
	// match based on taskname which is already in _userTask
	// not based on line number. this is because the line number
	// input by the user WILL NOT coincide with our storage line number
	// as he may be viewing the number based on search
	//	
	// copy the current data over to _previousData, in case user wants to undo
	// then modify the current data
	//
	//
}

void Storage::reset() {
	// simple stuff ah
	// copy _store into _previousStore
	// incase the user wants to undo
	// reset vector
	// reset txt file
}

void Storage::undo() {

	// make use of _previousTask to identify the command
	// make use of _previousData if command is modify
	// make use of _previousStore if command is reset

	// remember to undo from vector AND txt file
	// if function to be undone is 'add'
	// can simply use the deletefunction to undo
}

void archive() {

}

Task convertToTask(string data) {

	Task task;
	string taskName;
	string deadline;
	string priority;

	istringstream InStream(data);
	InStream >> taskName >> deadline >> priority;

	task.changeTaskName(taskName);
	task.changeDeadline(deadline);
	task.changePriority(priority);

	return task;
}

string convertToString(Task task) {
	ostringstream OutStream;
	OutStream << task.getTaskName<< " " << task.getDeadline<< " "<< task.getPriority<< endl;
	return OutStream.str();
}


